﻿using Ndns.ServerHost.Abstractions.Services;
using System;
using System.Collections.Generic;
using Ndns.ServerHost.Abstractions;
using System.Net;
using Ndns.ServerHost.Features;
using System.Threading;

namespace Ndns.ServerHost.Services
{
    class ContextFactoryServiceImpl : IContextFactoryService
    {
        public ConnectionInfo CreateConnectionInfo(DnsProtocol protocol, IPAddress localIpAddress, int localPort, IPAddress remoteIpAddress, int remotePort)
        {
            return new ConnectionInfoImpl
            {
                Protocol = protocol,
                LocalIpAddress = localIpAddress,
                LocalPort = localPort,
                RemoteIpAddress = remoteIpAddress,
                RemotePort = remotePort
            };
        }

        public DnsContext CreateContext(string traceIdentifier, ConnectionInfo connection, IEnumerable<KeyValuePair<Type, object>> builtInFeature, bool serviceCollectionReadOnly, CancellationToken cancellationToken)
        {
            var featureCollection = new FeatureCollection(builtInFeature, serviceCollectionReadOnly);
            var request = new DnsRequestImpl();
            var response = new DnsResponseImpl();
            var context = new DnsContextImpl(traceIdentifier, connection, featureCollection, request, response, cancellationToken);
            request.SetContext(context);
            response.SetContext(context);
            return context;
        }
    }
}
